package me.xplabs.model 
{
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.BaseBuild;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.MBuildInfo;
	import me.xplabs.model.skill.BaseSkill;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.SimpleObjPool;
	/**
	 * 建筑数据管理
	 * ...
	 * @author xiaohan
	 */
	public class BuildDataManager 
	{
		private var _buildPool:SimpleObjPool;
		private var _builds:Vector.<MBuildInfo>;
		public function BuildDataManager() 
		{
			_builds = new Vector.<MBuildInfo>();
			_buildPool = new SimpleObjPool(MBuildInfo, 1, 1);
		}
		
		public function update():void
		{
			var len:int = _builds.length;
			for (var i:int = len - 1; i >= 0 ; i--)
			{
				_builds[i].heartbeat();
			}
		}
		
		
		public function initBuildList(p_builds:Vector.<MBuildInfo>):void 
		{
			_builds.length = 0;
			var temps:Vector.<MBuildInfo> = p_builds.slice();
	
			var len:int = temps.length;
			for (var i:int = 0; i < len; i++) 
			{
				var baseBuild:BaseBuild = GameDataSingleton.i.getBaseBuildById(int(temps[i].resId));
				var skills:Vector.<BaseSkill> = new Vector.<BaseSkill>();
				for (var j:int = 0; j < baseBuild.skillIds.length; j++) 
				{
					skills.push(GameDataSingleton.i.getBaseSkillById(baseBuild.skillIds[j]));
				}
				addBuilding(baseBuild, temps[i].cellX, temps[i].cellY, skills,temps[i].soleId);
			}
		}
		
		public function addBuilding(p_baseBuild:BaseBuild, p_cellX:int, p_cellY:int, skills:Vector.<BaseSkill>, p_soleId:int):void
		{
			var tempBuild:MBuildInfo = new MBuildInfo(p_baseBuild.resId, p_cellX, p_cellY);
			tempBuild.soleId  = p_soleId;
			buildAssignment(p_baseBuild, tempBuild);
			//if (p_baseBuild.buildType != BuildType.BASE_BUILD) tempBuild.paths = _mMapData.paths;
			//else tempBuild.paths = null;
			tempBuild.initSkillList(skills);
			_builds.push(tempBuild);
			sendMsg(NotificationName.FIGHT_NOTIFICATION, tempBuild, NotificationFight.ADD_BUILD_COMPLETE);
		}
		
		/**
		 * 升级建筑物
		 * @param	p_id
		 */
		public function upgradeBuilding(p_soleId:int, p_baseBuild:BaseBuild, p_baseFlag:BaseFlag , skills:Vector.<BaseSkill>):void
		{
			var len:int = _builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_builds[i].soleId == p_soleId)
				{
						var tempBuild:MBuildInfo = _builds[i];
						buildAssignment(p_baseBuild, tempBuild);
						//if (p_baseBuild.buildType != BuildType.BASE_BUILD) tempBuild.paths = _mMapData.paths;
						//else tempBuild.paths = null;
						tempBuild.initSkillList(skills);
						sendMsg(NotificationName.FIGHT_NOTIFICATION, tempBuild, NotificationFight.UPGRADE_BUILD_COMPLETE);
					break;
				}
			}
		}
		
		/**
		 * 销毁建筑物
		 * @param	p_id
		 */
		public function destoryBuilding(p_soleId:int, p_builds:Vector.<MBuildInfo>):void
		{
			var len:int = _builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_builds[i].soleId == p_soleId)
				{
					var tempBuild:MBuildInfo = _builds[i];
					break;
				}
			}
			len = p_builds.length;
			for (i = 0; i < len; i++) 
			{
				if (p_builds[i].soleId == p_soleId)
				{
						var p_baseBuild:BaseBuild = p_builds[i];
						buildAssignment(p_baseBuild, tempBuild);
						//tempBuild.paths = null;
						tempBuild.initSkillList(null);
						sendMsg(NotificationName.FIGHT_NOTIFICATION, tempBuild, NotificationFight.DESTORY_BUILD_COMPLETE);
					break;
				}
			}
		}
		
		public function removeBuilding(p_building:MBuildInfo):void
		{
			sendMsg(NotificationName.FIGHT_NOTIFICATION, p_building.soleId, NotificationFight.REMOVE_BUILDING);
			//RolePositionManager.i.deleteCellActor(p_actor.prevCellPos,p_actor.curCellPos, p_actor.soleId);
			Globals.delSoleId(p_building.soleId);
			p_building.dispose();
			_buildPool.dispose(p_building);
			p_building = null;
			//Utils.print("敌人消失啦");
		}
		
		private function buildAssignment(build1:BaseBuild,build2:MBuildInfo):void
		{
			build2.id  			= build1.id;  			
			build2.title        = build1.title;
			build2.buildType    = build1.buildType;   
			build2.upgradeId    = build1.upgradeId;   
			build2.resId        = build1.resId;       
			build2.attack       = build1.attack;      
			build2.defense      = build1.defense;     
			build2.buildIcon    = build1.buildIcon;   
			build2.attackSpeed  = build1.attackSpeed; 
			build2.attackArea   = build1.attackArea;  
			build2.areaW        = build1.areaW;       
			build2.areaH        = build1.areaH;       
			build2.cdGap        = build1.cdGap;       
			build2.areaCellW    = build1.areaCellW;   
			build2.areaCellH    = build1.areaCellH;   
			build2.flag         = build1.flag;        
			build2.describe     = build1.describe;    
			build2.upgradeGold  = build1.upgradeGold; 
			build2.buildGold     = build1.buildGold;    
			build2.buildLevel     = build1.buildLevel;    
			build2.skillIds     = build1.skillIds;    
		}
		
		public function dispose():void
		{
			var len:int = _builds.length;
			for (var i:int = 0; i < len; i++) 
			{
				removeBuilding(_builds[i]);
			}
			_builds.length = 0;
		}
	}

}